package com.ifar.audiosdk.activity

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.ifar.audiosdk.R
import com.ifar.audiosdk.base.BaseActivity
import com.ifar.audiosdk.common.IfarConfig
import com.ifar.audiosdk.model.login.viewmodel.LoginViewModel
import com.ifar.audiosdk.utils.ARouterHandle
import com.ifar.audiosdk.utils.CommonUtils
import com.ifar.audiosdk.utils.showToast
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.include_view.*
import kotlinx.android.synthetic.main.include_view.view.*

@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<LoginViewModel>() {
    override fun getLayoutResId(): Int = R.layout.activity_login

    override fun initView() {
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        username.afterTextChanged {
            mViewModel.loginDataChanged(
                username.text.toString(),
                password.text.toString()
            )
        }

        password.apply {
            afterTextChanged {
                mViewModel.loginDataChanged(
                    username.text.toString(),
                    password.text.toString()
                )
            }

            setOnEditorActionListener { _, actionId, _ ->
                when (actionId) {
                    EditorInfo.IME_ACTION_DONE -> {
                        showLoading()
                        mViewModel.login(
                            username.text.toString(),
                            password.text.toString()
                        )
                    }
                }
                false
            }
        }

        login.setOnClickListener {
            showLoading()
            mViewModel.login(username.text.toString(), password.text.toString())
        }

        bt1.setOnClickListener {
            if (CommonUtils.isFastClick()) {
                showToast("111")
            }
        }

        bt2.setOnClickListener {
            if (CommonUtils.isFastClick()) {
                showToast("222")
            }
        }
    }

    override fun initData() {
        username.setText(IfarConfig.name)
        username.setText("devWx7")
        password.setText("123456")
    }

    override fun startObserve() {
        mViewModel.apply {
            //页面控件状态
            loginFormState.observe(this@LoginActivity, Observer {
                val loginState = it ?: return@Observer

                login.isEnabled = loginState.isDataValid

                if (loginState.usernameError != null) {
                    username.error = getString(loginState.usernameError!!)
                }
                if (loginState.passwordError != null) {
                    password.error = getString(loginState.passwordError!!)
                }
            })

            apiLoginSuccess.observe(this@LoginActivity, {
                hideLoading()
                ARouterHandle.toMain()
                finish()
            })

            apiError.observe(this@LoginActivity, {
                hideLoading()
                showToast(it)
            })
        }
    }

    override fun onBackPressed() {
        doubleOnClickExit()
    }

    fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
        this.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(editable: Editable?) {
                afterTextChanged.invoke(editable.toString())
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
        })
    }

}